<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

function thumb_name($path){
    $img = explode('.', $path);
    $aImg = $img;
    unset($aImg[count($aImg)-1]);
    $imgPath = implode('', $aImg) . '.thumb.' . $img[count($img)-1];
    return $imgPath;
}

function temp_image($name, $image_folder = NULL , $isShop=false, $is_thumb = false){
    if($is_thumb){
        $name = thumb_name($name);
    }
    if($isShop || $image_folder!=null){
        return base_url('uploads/public/' . $image_folder . '/' . TEMP_FOLDER_URL . $name);
    }
    return base_url('uploads/' . TEMP_FOLDER_URL . $name);
}

function is_image($path) {
    $a = getimagesize($path);
    $image_type = $a[2];
    if(in_array($image_type , array(IMAGETYPE_GIF , IMAGETYPE_JPEG ,IMAGETYPE_PNG , IMAGETYPE_BMP))) {
        return true;
    }
    return false;
}

/*
* get image from $path
*/
function upload($path, $is_thumb = true){
	$imgPath = "";
	if ($is_thumb) {
		$aImg = explode('/', $path);
		$img = explode('.', $aImg[count($aImg)-1]);
		unset($aImg[count($aImg)-1]);
		$imgPath = implode('/', $aImg) . '/' . $img[0] . '.thumb.' . $img[1];
	} else {
		$imgPath .= $path;
	}
	return base_url('uploads/' . $imgPath);
}
function upload_raw($img, $user_folder){
	return base_url('uploads/public/' . $user_folder . '/uploaded/' . $img);
}

/*
* get shop slide image
*/
function slide_upload($img, $shop_folder, $is_thumb = true){
	return base_url('uploads/public/' . $shop_folder . '/' . SHOP_SLIDE_IMAGE_URL . $img);
}

/*
* get shop slide image
*/
function raovat_upload($img, $shop_folder, $is_thumb = true){
	$imgPath = "";
	if ($is_thumb) {
		$img = explode('.', $img);
		$imgPath = $img[0] . '.thumb.' . $img[1];
	} else {
		$imgPath .= $img;
	}
	return base_url('uploads/public/' . $shop_folder . '/' . RAOVAT_IMAGE_URL . $imgPath);
}

/*
* get shop slide image
*/
function post_upload($img, $folder, $is_thumb = true){
	$imgPath = "";
	if ($is_thumb) {
		$img = explode('.', $img);
		$imgPath = $img[0] . '.thumb.' . $img[1];
	} else {
		$imgPath .= $img;
	}
	return base_url('uploads/public/' . $folder . '/' . POST_IMAGE_URL . $imgPath);
}

/*
* get advertise image
*/
function advertise_upload($img){
	return base_url('uploads/' . ADVERTISE_IMAGE_URL . $img);
}

/*
* get banner image
*/
function banner_upload($img, $shop_folder, $is_thumb = true){
	if($is_thumb){
		$str = '';
		$aImg = explode('.', $img);
		for($i=0;$i<count($aImg)-1;$i++){
			$str .= $aImg[$i];
		}
		$str .= '.thumb.' . $aImg[count($aImg)-1];
		return base_url('uploads/public/' . $shop_folder. '/' . SHOP_BANNER_IMAGE_URL  . $str);
	}
	return base_url('uploads/public/' . $shop_folder. '/' . SHOP_BANNER_IMAGE_URL . $img);
}

/*
* get product image
*/
function product_upload($img, $shop_folder, $is_thumb = true){
	if ($is_thumb) {
		$img = explode('.', $img);
		$img = $img[0] . '.thumb.' . $img[1];
	}
	return base_url('uploads/public/' . $shop_folder . '/' . PRODUCT_IMAGE_URL . $img);
}

/*
* get avatar_upload
*/
function avatar_upload($img, $shop_folder, $is_thumb = true){
	if ($is_thumb) {
		$img = explode('.', $img);
		$img = $img[0] . '.thumb.' . $img[1];
	}
	return base_url('uploads/public/' . $shop_folder . '/personal/' . $img);
}

/*
* Get list image for modal select image from path
*/
function upload_modal_get($path){
	$aImageTemp = get_filenames($path);
	$aImage = array();
	foreach ($aImageTemp as &$i) {
		if (is_image($path.$i) && strpos($i, '.thumb') === false) {
			array_push($aImage, $i);
		}
	}
	return $aImage;
}
?>